package com.ljl.ningwei.service.impl;

import com.ljl.ningwei.entity.Category;
import com.ljl.ningwei.mapper.CategoryMapper;
import com.ljl.ningwei.service.CategoryService;
import com.ljl.ningwei.common.utils.TimeUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 * 分类表 服务实现类
 * </p>
 *
 * @author ljl
 * @since 2025-03-24
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    private final CategoryMapper categoryMapper;

    @Override
    public Category getById(Long id) {
        return Optional.ofNullable(categoryMapper.getById(id))
                .orElseThrow(() -> new RuntimeException("查询分类 id " + id + " 结果为空"));
    }

    @Override
    public Category extractById(Long id) {
        return Optional.ofNullable(categoryMapper.extractById(id))
                .orElseThrow(() -> new RuntimeException("查询分类 id " + id + " 结果为空"));
    }

    @Override
    public void delete(Long id) {
        // 插入操作并直接返回布尔值
        if (categoryMapper.delete(id) == 0) {
            throw new RuntimeException("分类id:" + id + "不存在,删除异常");
        }
    }

    @Override
    public List<Category> list() {
        List<Category> categoryList = categoryMapper.list();
        // 如果为空
        return categoryList;
    }

    @Override
    public Long edit(Long id, String name, String imageUrl) {
        if (id == null) {
            // 插入添加校验
            insertValied(id, name, imageUrl);
        } else {
            // 更新操作前，先查询物料是否存在
            if (categoryMapper.extractById(id) == null) {
                throw new RuntimeException("物料" + id + " 不存在，更新失败");
            }
        }
        // 对象转化
        Category category = toDTO(id, name, imageUrl);
        int result = id == null ? categoryMapper.insert(category) : categoryMapper.update(category);
        if (result == 0) {
            throw new RuntimeException("物料编辑失败");
        }
        return category.getId();
    }

    @Override
    public List<Category> listByIds(String categoryIds) {
        List<Category> categoryList = categoryMapper.listByIds(categoryIds);
        //   "1,2,3,4"
        int length = categoryIds.split(",").length;
        if (CollectionUtils.isEmpty(categoryList) || length != categoryList.size()) {
            throw new RuntimeException("批量分类查询不符合预期");
        }
        return categoryList;
    }

    @Override
    public List<Long> getIdsByName(String keyname) {
        List<Long> ids = categoryMapper.getIdsByName(keyname);
        return ids;
    }

    @Override
    public List<Category> listByParentId(Integer parentId) {
        return categoryMapper.listByParentId(parentId);
    }

    private Category toDTO(Long id, String name, String imageUrl) {
        return Category.builder()
                .id(id)
                .name(name)
                .imageUrl(imageUrl)
                .createTime(id == null ? TimeUtil.getTime() : null)
                .updateTime(TimeUtil.getTime())
                .build();
    }

    private void insertValied(Long id, String name, String imageUrl) {
        //前置校验进行检查
        // 校验名称
        if (StringUtils.isBlank(name)) {
            throw new RuntimeException("名称不能为空");
        }
        // 校验产品名称
        if (StringUtils.isBlank(imageUrl)) {
            throw new RuntimeException("图片路径不能为空");
        }
    }


}
